import { mapGetters } from 'vuex'
// import moment from 'moment'
import common from '../common.js'
import { formatDate, formatTen } from '../../assets/js/help.js'
export default {
  mixins: [common],
  props: {
    searchList: Array,
    singleWidth: {
      type: [String, Number],
      default: '140'
    },
    tableBoxId: {
      type: String,
      default: ''
    },
    isSetSearch: {
      type: [String, Boolean],
      default: true
    }
  },
  data () {
    return {
      isSingle: true,//是单项还是多项选择条件，true为单项，false为多项
      isMoreFilters: false,//多项选择条件下， 是否是显示更多选择条件
      singleIndex: 0,
      isChangeH: false,
      selectionList: this.searchList
    }
  },
  computed: {
    ...mapGetters ({
      isFullScreenTable: 'getFullScreenTable',
      isMask: 'getIsMask'
    })
  },
  created () {
    this.initSetSearchList();
    this.confirmSearch();
  },
  methods: {
    initFn () {
      this.isSingle = true;
      this.isMoreFilters = false;
      this.singleIndex = 0;
    },
    initSetSearchList () {
      this.isReload = true;
      if(this.selectionList[0].index == undefined) {
        this.selectionList.map(function(item, index){
          item.index = index;
          item.isShow = false;
          return item;
        })
      }
      this.$nextTick(function(){
        this.isReload = false;
      })
    },
    changeHeight (isSingleMulti) {
      if(isSingleMulti) {
        this.isSingle = !this.isSingle;
      } else {
        this.isMoreFilters = !this.isMoreFilters;
      }
      this.isChangeH = !this.isChangeH;
      this.$emit('changeHeight', this.isChangeH);
    },
    //获取某个月份的天数
    getCountDays (date) {
      var curDate = date;
      /* 获取当前月份 */
      var curMonth = curDate.getMonth();
      /*  生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */
      curDate.setMonth(curMonth + 1);
      curDate.setDate(0);
      /* 返回当月的天数 */
      return curDate.getDate();
    },
    confirmSearch () {
      let selectArr = this.selectionList.slice(0);
      let params = {};
      for(let i = 0, len = selectArr.length; i < len; i ++) {
        let thisObj = selectArr[i];
        let Val = thisObj.value;
        let name = thisObj.name;
        if('datePicker' == thisObj.type || 'timePicker' == thisObj.type) {
          let propArr = thisObj.propName.split(',');
          if(Val && Val.constructor == Array && Val.length > 0) {
            if('timePicker' == thisObj.type && Val[0]) {
              let _dateVal0 = new Date(Val[0]);
              let _dateVal1 = new Date(Val[1]);
              let _hour0 = _dateVal0.getHours();
              let _minute0 = formatTen(_dateVal0.getMinutes());
              let _second0 = formatTen(_dateVal0.getSeconds());
              let _hour1 = _dateVal1.getHours();
              let _minute1 = formatTen(_dateVal1.getMinutes());
              let _second1 = formatTen(_dateVal1.getSeconds());
              let year = moment().year();
              let month = formatTen(moment().month());
              let day = formatTen(moment().date());
              let dateVal0 = new Date(year, month, day, _hour0, _minute0, _second0);              
              let dateVal1 = new Date(year, month, day, _hour1, _minute1, _second1); 
              Val[0] = dateVal0;            
              Val[1] = dateVal1;            
            }
            if(Val[0]) {
              params[propArr[0]] = formatDate(Val[0]);
            } else {
              params[propArr[0]] = undefined;
            }
            if(Val[1]) {
              if('daterange' == thisObj.pickerType) {
                let _Val1 = Val[1];
                _Val1 = _Val1.getFullYear() + '-' + formatTen(_Val1.getMonth() + 1) + '-' + formatTen(_Val1.getDate()) + ' 23:59:59';
                params[propArr[1]] = _Val1;
              } else {
                params[propArr[1]] = formatDate(Val[1]);
              }
              // console.log(Val[1])
            } else {
              params[propArr[1]] = undefined;
            }
          } else {
            if(Val) {
              let year = Val.getFullYear();
              let month = formatTen(Val.getMonth() + 1);
              let _start = '';
              let _end = '';
              if('year' == thisObj.pickerType) {
                _start = year + '-01-01 00:00:00';
                _end = year + '-12-31 23:59:59';
              } else if('month' == thisObj.pickerType) {
                let days = this.getCountDays(Val);
                _start = year + '-' + month + '-01 00:00:00';
                _end = year + '-' + month + '-' + days + ' 23:59:59';
              }
              params[propArr[0]] = _start;
              params[propArr[1]] = _end;
            }
          }
        } else if ('doubleInput' == thisObj.type) {
          let names = thisObj.names;
          let values = thisObj.values;
          if(values[0] !== '') { params[names[0]] = values[0]; }
          if(values[1] !== '') { params[names[1]] = values[1]; }
        } else {
          if(Val) {
            params[name] = Val;
          }
        } 
      }
      this.$emit('confirmSearch', params);
    }
  }
}